"""
@author: xiangping
@contact: xiangpingbu@gmail.com
@time: 2020/1/4 3:47 下午
@file: gaolu_score_model.py
@Desc: GaoLuScoreModel
"""

from flask import current_app

from app.models.bo.gaolu.gaolu_model_score_bo import GaoLuModelScoreBO
from app.services.models.base_model import BaseModel


class GaoLuScoreModelOld(BaseModel):
    def __init__(self):
        super().__init__(model_name="gaolu_score_model")

    def process(self, gaolu_model_input_bo):
        try:

            Si_Ti = gaolu_model_input_bo.CG_LT_GL_GL04_Si_Ti
            RLB = gaolu_model_input_bo.CG_LT_GL_GL04_RLB
            LLT = gaolu_model_input_bo.CG_LT_GL_GL04_FLiLunTie

            Si_Ti_sub_score = self._get_Si_Ti_sub_score(Si_Ti)
            RLB_sub_score = self._get_RLB_sub_score(RLB)
            LLT_sub_score = self._get_LLT_sub_score(LLT)

            score1 = Si_Ti_sub_score + RLB_sub_score + LLT_sub_score
            score2 = score1
            if Si_Ti_sub_score == 0 or RLB_sub_score == 0 or LLT_sub_score == 0:
                score2 = 0
            status = self._get_status_from_score(score2)
            return GaoLuModelScoreBO(score1=score1,
                                     score2=score2,
                                     status=status)
        except Exception as e:
            current_app.logger.info(e)

    def _get_status_from_score(self, score):
        if score > 90:
            return '优秀'
        elif 80 <= score < 90:
            return '正常'
        elif 60 <= score < 80:
            return '波动'
        else:
            return '失常'

    def _get_Si_Ti_sub_score(self, Si_Ti):
        if Si_Ti < 0.2:
            Si_Ti_score = 0
        elif 0.2 <= Si_Ti < 0.21:
            Si_Ti_score = 40
        elif 0.21 <= Si_Ti < 0.22:
            Si_Ti_score = 39.6
        elif 0.22 <= Si_Ti < 0.23:
            Si_Ti_score = 39.2
        elif 0.23 <= Si_Ti < 0.24:
            Si_Ti_score = 38.6
        elif 0.24 <= Si_Ti < 0.25:
            Si_Ti_score = 38
        elif 0.25 <= Si_Ti < 0.26:
            Si_Ti_score = 36.8
        elif 0.26 <= Si_Ti < 0.27:
            Si_Ti_score = 35.6
        elif 0.27 <= Si_Ti < 0.28:
            Si_Ti_score = 34.4
        elif 0.28 <= Si_Ti < 0.29:
            Si_Ti_score = 33.2
        elif 0.29 <= Si_Ti < 0.30:
            Si_Ti_score = 32
        elif 0.30 <= Si_Ti < 0.31:
            Si_Ti_score = 30.8
        elif 0.31 <= Si_Ti < 0.32:
            Si_Ti_score = 30
        elif 0.32 <= Si_Ti < 0.33:
            Si_Ti_score = 29.2
        elif 0.33 <= Si_Ti < 0.34:
            Si_Ti_score = 28.6
        elif 0.34 <= Si_Ti < 0.35:
            Si_Ti_score = 28
        elif 0.35 <= Si_Ti < 0.36:
            Si_Ti_score = 26
        elif 0.36 <= Si_Ti < 0.37:
            Si_Ti_score = 25.2
        elif 0.37 <= Si_Ti < 0.38:
            Si_Ti_score = 24.8
        elif 0.38 <= Si_Ti < 0.39:
            Si_Ti_score = 24.4
        elif 0.39 <= Si_Ti < 0.40:
            Si_Ti_score = 24
        elif Si_Ti >= 0.40:
            Si_Ti_score = 0
        else:
            Si_Ti_score = 0

        return Si_Ti_score

    def _get_RLB_sub_score(self, RLB):
        if RLB < 470:
            RLB_score = 0
        elif 470 <= RLB < 480:
            RLB_score = 30
        elif 480 <= RLB < 482:
            RLB_score = 29.7
        elif 482 <= RLB < 484:
            RLB_score = 29.25
        elif 484 <= RLB < 486:
            RLB_score = 28.65
        elif 486 <= RLB < 488:
            RLB_score = 27.9
        elif 488 <= RLB < 490:
            RLB_score = 27
        elif 490 <= RLB < 492:
            RLB_score = 26.4
        elif 492 <= RLB < 494:
            RLB_score = 25.8
        elif 494 <= RLB < 496:
            RLB_score = 25.2
        elif 496 <= RLB < 498:
            RLB_score = 24.6
        elif 498 <= RLB < 500:
            RLB_score = 24
        elif 500 <= RLB < 502:
            RLB_score = 23.4
        elif 502 <= RLB < 504:
            RLB_score = 22.65
        elif 504 <= RLB < 506:
            RLB_score = 22.05
        elif 506 <= RLB < 508:
            RLB_score = 21.45
        elif 508 <= RLB < 510:
            RLB_score = 21
        elif 510 <= RLB < 514:
            RLB_score = 19.95
        elif 514 <= RLB < 518:
            RLB_score = 19.05
        elif 518 <= RLB < 522:
            RLB_score = 18.45
        elif 522 <= RLB < 526:
            RLB_score = 18.15
        elif 526 <= RLB < 530:
            RLB_score = 18
        elif 530 <= RLB:
            RLB_score = 0
        else:
            RLB_score = 0

        return RLB_score

    def _get_LLT_sub_score(self, LLT):
        if LLT > 275:
            LLT_score = 0
        elif 260 < LLT <= 275:
            LLT_score = 30
        elif 255 < LLT <= 260:
            LLT_score = 29.7
        elif 250 < LLT <= 255:
            LLT_score = 28.05
        elif 245 < LLT <= 250:
            LLT_score = 27
        elif 240 < LLT <= 245:
            LLT_score = 26.7
        elif 235 < LLT <= 240:
            LLT_score = 24.75
        elif 230 < LLT <= 235:
            LLT_score = 24
        elif 225 < LLT <= 230:
            LLT_score = 23.85
        elif 220 < LLT <= 225:
            LLT_score = 21.75
        elif 215 < LLT <= 220:
            LLT_score = 21
        elif 210 < LLT <= 215:
            LLT_score = 20.7
        elif 205 < LLT <= 210:
            LLT_score = 18.9
        elif 200 < LLT <= 205:
            LLT_score = 18
        elif LLT <= 200:
            LLT_score = 0
        else:
            LLT_score = 0

        return LLT_score


# 用导出module的方法创建单例
gaolu_score_model = GaoLuScoreModelOld()
